package cm309.BookingSystem.vm;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class VenueListHandler extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			Connection conn = null;
			try {
				System.out.println("start to get connection...");
				conn = VenueDao.getConnection();
				System.out.println("finish to get  connection...");

				PreparedStatement ps;
				ps = conn.prepareStatement("SELECT * FROM BK_VM_VENUE");
				ResultSet rs = ps.executeQuery();
				List<Venue> list = new ArrayList<Venue>();
				while (rs.next()){
					Venue venue = new Venue();
					System.out.println("abc "+ rs.getInt(1));
					venue.setId(rs.getInt(1));
					venue.setName(rs.getString(2));
					list.add(venue);
				}
				req.setAttribute("venueList", list);
				RequestDispatcher dispatcher = req.getRequestDispatcher("listVenue.jsp");
				dispatcher.forward(req, resp);
			}catch (SQLException e) {
				e.printStackTrace();
				
			}finally {
				conn.close();
			}
			
		}catch (Exception e){
			e.printStackTrace();
		}	
		
	}

}
